<template>
  <el-dialog
    :title="!dataForm.activityname ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动开始的时间" prop="begintime">
      <el-input v-model="dataForm.begintime" placeholder="活动开始的时间"></el-input>
    </el-form-item>
    <el-form-item label="活动结束的时间" prop="endtime">
      <el-input v-model="dataForm.endtime" placeholder="活动结束的时间"></el-input>
    </el-form-item>
    <el-form-item label="活动地点" prop="address">
      <el-input v-model="dataForm.address" placeholder="活动地点"></el-input>
    </el-form-item>
    <el-form-item label="参与人数" prop="participant">
      <el-input v-model="dataForm.participant" placeholder="参与人数"></el-input>
    </el-form-item>
    <el-form-item label="活动详情" prop="detail">
      <el-input v-model="dataForm.detail" placeholder="活动详情"></el-input>
    </el-form-item>
    <el-form-item label="奖品" prop="prize">
      <el-input v-model="dataForm.prize" placeholder="奖品"></el-input>
    </el-form-item>
    <el-form-item label="活动是否有效" prop="valid">
      <el-input v-model="dataForm.valid" placeholder="活动是否有效"></el-input>
    </el-form-item>
    <el-form-item label="活动开始报名时间" prop="applystarttime">
      <el-input v-model="dataForm.applystarttime" placeholder="活动开始报名时间"></el-input>
    </el-form-item>
    <el-form-item label="活动结束报名的时间" prop="applyendtime">
      <el-input v-model="dataForm.applyendtime" placeholder="活动结束报名的时间"></el-input>
    </el-form-item>
    <el-form-item label="活动发布者的名字" prop="publisher">
      <el-input v-model="dataForm.publisher" placeholder="活动发布者的名字"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          activityname: 0,
          begintime: '',
          endtime: '',
          address: '',
          participant: '',
          detail: '',
          prize: '',
          valid: '',
          applystarttime: '',
          applyendtime: '',
          publisher: ''
        },
        dataRule: {
          begintime: [
            { required: true, message: '活动开始的时间不能为空', trigger: 'blur' }
          ],
          endtime: [
            { required: true, message: '活动结束的时间不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '活动地点不能为空', trigger: 'blur' }
          ],
          participant: [
            { required: true, message: '参与人数不能为空', trigger: 'blur' }
          ],
          detail: [
            { required: true, message: '活动详情不能为空', trigger: 'blur' }
          ],
          prize: [
            { required: true, message: '奖品不能为空', trigger: 'blur' }
          ],
          valid: [
            { required: true, message: '活动是否有效不能为空', trigger: 'blur' }
          ],
          applystarttime: [
            { required: true, message: '活动开始报名时间不能为空', trigger: 'blur' }
          ],
          applyendtime: [
            { required: true, message: '活动结束报名的时间不能为空', trigger: 'blur' }
          ],
          publisher: [
            { required: true, message: '活动发布者的名字不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.activityname = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.activityname) {
            this.$http({
              url: this.$http.adornUrl(`/lovegoing_admin/auditactivity/info/${this.dataForm.activityname}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.begintime = data.auditactivity.begintime
                this.dataForm.endtime = data.auditactivity.endtime
                this.dataForm.address = data.auditactivity.address
                this.dataForm.participant = data.auditactivity.participant
                this.dataForm.detail = data.auditactivity.detail
                this.dataForm.prize = data.auditactivity.prize
                this.dataForm.valid = data.auditactivity.valid
                this.dataForm.applystarttime = data.auditactivity.applystarttime
                this.dataForm.applyendtime = data.auditactivity.applyendtime
                this.dataForm.publisher = data.auditactivity.publisher
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/lovegoing_admin/auditactivity/${!this.dataForm.activityname ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'activityname': this.dataForm.activityname || undefined,
                'begintime': this.dataForm.begintime,
                'endtime': this.dataForm.endtime,
                'address': this.dataForm.address,
                'participant': this.dataForm.participant,
                'detail': this.dataForm.detail,
                'prize': this.dataForm.prize,
                'valid': this.dataForm.valid,
                'applystarttime': this.dataForm.applystarttime,
                'applyendtime': this.dataForm.applyendtime,
                'publisher': this.dataForm.publisher
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
